package ProjectTeam39.Domains;
import java.io.*;
import java.util.*;
//import java.util.Scanner;

public class Clientelle
{
	
	//should keep a list of clients,
	//possibly in XML form.
	//for now it will be writing to a .txt file created
	//in the directory where the .java files are
	//(maybe in it's own folder?)
	protected FileOutputStream writer;
	protected PrintStream out;
	protected String filename = "clientelle.txt";
	protected String fullfilepath;
	protected UserList users;
	protected String dir;
	protected String sep;
	protected ArrayList<Patient> theClients;
	
	
	
	public Clientelle(UserList theUsers)
	{
			theClients = new ArrayList<Patient>();
			Patient testPatient = new Patient("Kyle Kukshtel", Long.parseLong("4789879924"), "109 Ashley Court", new MedicalInfo("My Pharmacy", "My Insurance", "My Alergies Are Girls"), "myusername", "mypassword");
			this.createClient(testPatient);
			this.users = theUsers;
	}
	
	public ArrayList<Patient> getUsers(){
		return theClients;
	}
	
	void linkUserList(UserList users)
	{
		this.users = users;
		dir = users.getDir();
		sep = users.getSep();
		fullfilepath = dir+sep+filename;
		
		try
		{
			writer = new FileOutputStream(fullfilepath);
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
		}
	}
	
	//Array list that holds patients, functions that can index into each
	//patient in that list and get name/user/pass
	boolean createClient(Patient thePatient)
	{
		//needs to assign a unique user id to each person for easier searching
		try
		{
			theClients.add(thePatient);
		}
		catch (Exception e)
		{
			return false;
		}
		return true;
	}
	
	Patient getPatient(String name)
	{
		boolean found = false;
		int index = 0;
		for(int i=0;i<theClients.size();i++)
		{
			while(found == false)
			{
				if(theClients.get(i).getName().equalsIgnoreCase(name))
				{
					found = true;
					index = i;
				}
			}
		}
		if(found == true)
		{
			return theClients.get(index);
		}
		else
		{
			System.out.println("Client Not Found");
			return null;
		}
		
	}
	
	boolean findPatient(String name) //returns index of patient if found
	{
		boolean found = false;
		for(int i=0;i<theClients.size();i++)
		{
			while(found == false)
			{
				if(theClients.get(i).getName().equalsIgnoreCase(name))
				{
					found = true;
				}
			}
		}
		if(found == true)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	int findPatientIndex(String name) //returns index of patient if found
	{
		boolean found = false;
		int index = 0;
		for(int i=0;i<theClients.size();i++)
		{
			while(found == false)
			{
				if(theClients.get(i).getName().equalsIgnoreCase(name))
				{
					found = true;
					index = i;
				}
			}
		}
		if(found == true)
		{
			return index;
		}
		else
		{
			return index; //may need to change this
		}
		
	}
	
	boolean deletePatient(String name)
	{
		try{
			if(findPatient(name) == true)
			{
				theClients.remove(findPatientIndex(name));
			}
			if(findPatient(name) == false)
			{
				return false;
			}
		}
		catch(Exception e)
		{
			return false;
		}
		return true;
		
	}
	
}
